package com.xiangxiangjd.kj.papersstar.serviceImpl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiangxiangjd.kj.papersstar.bean.Answer;
import com.xiangxiangjd.kj.papersstar.bean.Topic;
import com.xiangxiangjd.kj.papersstar.bean.result.ResultPage;
import com.xiangxiangjd.kj.papersstar.controller.helper.$;
import com.xiangxiangjd.kj.papersstar.controller.model.ApiResultModel;
import com.xiangxiangjd.kj.papersstar.mapper.TopicMapper;
import com.xiangxiangjd.kj.papersstar.service.AnswerService;
import com.xiangxiangjd.kj.papersstar.service.TopicService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 肖守凯
 * @since 2021-01-21
 */
@Service
@Slf4j
public class TopicServiceImpl extends ServiceImpl<TopicMapper, Topic> implements TopicService {


    @Autowired
    private AnswerService answerService;

    /**
     * 添加Topic
     *
     * @param topic 前端传进的参数
     * @return 返回统一信息
     */
    @Override
    public ApiResultModel addTopic(Topic topic) {
        if (this.save(topic)) {
            return $.success();
        } else {
            return $.error("添加失败");
        }
    }

    /**
     * 编辑跳转页面的回显数据Topic
     *
     * @param id 前端传进的参数
     * @return 返回实体类
     */
    @Override
    public Topic selectTopicByIdTopic(String id) {
        return this.getById(id);
    }


    /**
     * 编辑Topic
     *
     * @param topic 前端传进的参数
     * @return 返回统一信息
     */
    @Override
    public ApiResultModel editTopic(Topic topic) {
        if (this.updateById(topic)) {
            return $.success();
        } else {
            return $.error("编辑失败");
        }
    }

    /**
     * 删除一条Topic
     *
     * @param ids 前端传进的ID
     * @return 返回统一信息
     */
    @Override
    public ApiResultModel delTopics(List ids) {

        int i = baseMapper.deleteBatchIds(ids);

        return $.success(i);

    }

    /**
     * 分页查询Topic
     *
     * @param page  页码
     * @param limit 查几条
     * @param topic 前端传进的参数
     * @return 返回统一分页信息
     */
    @Override
    public ResultPage findTopicByPage(Integer page, Integer limit, Topic topic) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("page", (page - 1) * limit);
        paramMap.put("limit", limit);
        paramMap.put("topic", topic);
        List<Topic> list = baseMapper.selectMyPage(paramMap);
        Integer count = baseMapper.countMyPage(paramMap);
        ResultPage<Topic> resultPage = new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }

    @Transactional
    @Override
    public ApiResultModel addTopicAnswer(Topic topic) {
        log.info("添加了试题:" + topic.getAnswers().toString());
        log.info("添加了试题:" + topic.toString());
        int insert = baseMapper.insert(topic);
        if (insert != 1) {
            throw new RuntimeException("插入失败 请重新导入");
        }
        //获取用户提交的所有答案
        List<Answer> answers = topic.getAnswers();
        for (int i = 0; i < answers.size(); i++) {
            answers.get(i).setTopicId(topic.getId());
        }
        boolean b = answerService.saveBatch(answers);
        if (b) throw new RuntimeException("插入失败 请重新插入");
        return $.success("插入成功");
    }
}
